package com.sne.common.util;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * redis生成全局id
 *
 * @author fanghh
 * @date 2020/10/30
 */
@Component
public class IdGeneratorUtil implements InitializingBean {

    @Autowired
    private RedisTemplate redisTemplate;

    private static RedisTemplate<String,String> template;

    private static final String ID_KEY = "id:generator:log";

    /**
     * 生成唯一id
     *
     * @return Long
     */
    public static Long incrementId() {
        return template.opsForValue().increment(ID_KEY);
    }

    @Override
    public void afterPropertiesSet() {
        template = redisTemplate;
    }
}
